#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    vector<int> cnt(n + 1);
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        cnt[a[i]]++;
    }
    queue<int> q;
    for(int i = 1; i <= n; i++) if(cnt[i] == 0) q.push(i);
    int ans = 2;
    vector<int>st(n + 1);
    while(q.size()) {
        vector<int> t;
        while(q.size()){
            t.push_back(q.front());
            q.pop();
        }
        
        int ct = 0;
        for(auto x : t){
            // cout << x << ' ';
            if(st[x]) continue;
            cnt[a[x]]--;
            if(cnt[a[x]] == 0) ct++, q.push(a[x]);
            st[x] = true;
            
        }
        // cout <<'\n';
        ans++;
        if(ct == 0) break;
    }
    cout << ans << endl;
    

}

int main()
{
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}